{{ define "head-end" }}
{{- $titleKey := dict "name" "title" "weight" 0.8 -}}
{{- $contentKey := dict "name" "content" "weight" 0.5 -}}
{{- $tagsKey := dict "name" "tags" "weight" 0.3 -}}
{{- $categoriesKey := dict "name" "categories" "weight" 0.3 -}}
{{- $seriesKey := dict "name" "series" "weight" 0.3 -}}
{{- $keys := slice $titleKey $contentKey $tagsKey $categoriesKey $seriesKey -}}
{{- $options := dict "ignoreLocation" true "includeMatches" true "includeScore" true "keys" $keys -}}
{{- $optionWhitelist := slice "ignoreLocation" "isCaseSensitive" "minMatchCharLength" "threshold" "distance" "useExtendedSearch" -}}
{{- with .Site.Params.search.fuse -}}
  {{- range $key, $value := . -}}
    {{- range $optionWhitelist -}}
      {{- if eq $key (lower .) -}}
        {{- $options = merge $options (dict . $value) -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

<script>
  window.searchResultContentWordCount = parseInt('{{ default 240 .Site.Params.search.resultContentWordCount }}');
  window.searchPaginate = parseInt('{{ default 10 .Site.Params.search.paginate }}');
  window.fuseOptions = JSON.parse('{{ $options | jsonify | safeHTML }}');
</script>
{{- $script := resources.Get "js/search.js" | resources.Fingerprint -}}
<script src="{{ $script.Permalink }}" integrity="{{ $script.Data.Integrity }}" crossorigin="anonymous"></script>
{{ end }}

{{ define "content" }}
<div class="search container">
  <form id="searchForm" class="search-form mb-3">
    <div class="input-group input-group-lg">
      <button class="btn btn-search disabled position-absolute left-0" type="submit"><i class="fas fa-fw fa-search"></i></button>
      <input class="form-control rounded-pill ps-5" name="q" type="search" aria-label="Search">
    </div>
  </form>
  <div class="search-stat mb-3 me-1">
    <i id="loadingSpinner" class="fas fa-fw fa-spin fa-spinner"></i>
    <span id="searchStat"></span>
  </div>
  <div id="searchResults" class="search-results mb-3"></div>
  <div class="text-center mb-3">
    <button class="btn d-none" type="button" id="btnLoadMore">{{ i18n "load_more" }}</button>
  </div>
</div>
<script type="text/html" id="templateResult">
  <div class="search-result surface" id="{{ print "{{id}}" }}">
    <h2 class="search-result-title mb-2">
      <a {{ printf "href={{permalink}}" | safeHTMLAttr }}>{{ print "{{title}}" }}</a>
    </h2>
    <div class="search-result-taxonomies mb-3">
      <span class="search-result-score search-result-taxonomy">
        <span {{ printf "style=\"%s\"" "width: {{{score}}}%" | safeHTMLAttr }}></span>
      </span>
      <span class="search-result-taxonomy">{{ printf "{{date}}" }}</span>
      {{- $baseURL := absLangURL "" -}}
      {{- $placeholder := "{{#url}}{{.}}{{/url}}" -}}
      {{- range slice "series" "categories" "tags" -}}
      {{ printf "{{#%s}}" . }}
      <a {{ printf "href=\"%s%s/%s/\"" $baseURL . $placeholder | safeHTMLAttr }} class="search-result-taxonomy">{{ printf "{{.}}" }}</a>
      {{ printf "{{/%s}}" . }}
      {{- end -}}
    </div>
    <div class="search-result-content mb-3">{{ print "{{{content}}}" }}</div>
  </div>
</script>
<script type="text/html" id="templateMissingKeywords">
  {{ i18n "search_missing_keywords" }}
</script>
<script type="text/html" id="templateNoResults">
  {{ i18n "search_no_results" }}
</script>
<script type="text/html" id="templateStat">
  {{ i18n "search_stat" (dict "Total" "<span class=\"text-accent\">{{total}}</span>") | safeHTML }}
</script>
{{ end }}
